package com.liu.servlet;

import javax.servlet.*;
import java.io.IOException;

public class MyFilter implements Filter {
    private String encoding="UTF-8";

    /**
     * 初始化(服务器启动时调用，读取web.xml中的参数)
     * @param filterConfig
     * @throws ServletException
     */
    public void init(FilterConfig filterConfig) throws ServletException {
        this.encoding=filterConfig.getInitParameter("初始化别名");
    }

    /**
     * 过滤
     * @param servletRequest
     * @param servletResponse
     * @param filterChain
     * @throws IOException
     * @throws ServletException
     */
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding(this.encoding);
        servletResponse.setCharacterEncoding(this.encoding);
        //放行(到servlet下一个过滤器)。访问对应的页面和页面调用，和url-pattern相关
        filterChain.doFilter(servletRequest,servletResponse);
    }

    /**
     * 销毁(服务器关闭时调用)
     */
    public void destroy() {

    }
}
